#!/usr/bin/python

#
# == Synopsis
#
# Creates a list of all the packages in pkg-java svn which have been
# updated and need to be uploaded to Debian.
#
# == Usage
#
# updated_list_pkg_java.py > JavaUpdates.wiki
#
# == Author
#
# Varun Hiremath <varunhiremath@gmail.com>
#
# == Copyright
#
# Copyright (c) 2007 Varun Hiremath
# Licensed under the terms of the General Public license (GPL).
#

from BeautifulSoup import *
import urllib2, os
import threading, time

DHS="http://dehs.alioth.debian.org/maintainer.php?name="
#SVN_BASE="svn+ssh://localhost/svn/pkg-java/trunk/"
SVN_BASE="svn://svn.debian.org/svn/pkg-java/trunk/"
WSVN_BASE="http://svn.debian.org/wsvn/pkg-java/trunk/"
SVN_NAME="pkg-java"

Header="""#summary A list of updated packages on pkg-java svn
#labels Featured

Do not edit this file because it is automatically created by the script
[http://bollin.googlecode.com/svn/Bollin/updated_list_pkg_java.py updated_list_pkg_java.py] and
will be overwritten!

= List of Packages =

"""

print Header

def output(cmd):
    pipe = os.popen('{ ' + cmd + '; } 2> /dev/null', 'r')
    return pipe

class PackageInfo( threading.Thread ):
    def __init__(self, package):
        self.package = package
        threading.Thread.__init__ ( self )
    
    def run(self):
        try:
            Changelog = output("svn cat %s/%s/debian/changelog | dpkg-parsechangelog -l- " % (SVN_BASE, self.package)).readlines()
            SOURCE = Changelog[0].split(":")[-1].strip()
        except:
            return

        try:
            u = urllib2.urlopen(DHS+SOURCE)
            s = u.read()
            soup = BeautifulSoup(s)
            dhs_data = soup.findAll('li')
            status = []
            for item in dhs_data:
                status.append(str(item).replace("<li>","").replace("</li>","").replace("<b>","").replace("</b>",""))
            dhs_map = {}
            for pair in status:
                dhs_map[pair.split(":")[0].strip()] = pair.split(":")[1].strip()

            Debian_Version = dhs_map["Debian Version"]
            try:
                Upstream_Version = dhs_map["Upstream Version"]
            except:
                pass
        except:
            try:
                Debian_Version = output("apt-cache madison %s|grep unstable|awk '{ print $3 }'|uniq" % SOURCE).readlines()[0].strip()
            except:
                Debian_Version = "None"
                
        try:
            Bollin_Version = Changelog[1].split(":")[-1].strip()
            if cmp(Bollin_Version, Debian_Version)==1 or Debian_Version=="None":
                Changes = Changelog[5:]
                print " # [%s/%s/debian %s] " % (WSVN_BASE, self.package, self.package)
                print "   * %s Version: %s" % (SVN_NAME, Bollin_Version)
                print "   * Debian Version: %s" % Debian_Version
                try:
                    print "   * Upstream Version: %s" % Upstream_Version
                except:
                    pass
                LOG = []
                for line in Changes:
                    if line.strip()[0:4] in ["Date", "Chan", "Clos"]:
                        print "   * %s" % line.strip()
                    else:
                        LOG.append(line)

                print "     {{{"
                for line in LOG:
                        print "     %s" % line.strip()
                print "     }}}"
        except:
            return
    
packages = output("svn list %s" % SVN_BASE).readlines()

for package in packages:
    package = package.strip()[:-1]
    PackageInfo(package).start()
    time.sleep(0.2)
